package ch.fusun.baron.basic.clientui.coa;

import org.eclipse.swt.SWT;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Control;
import org.eclipse.swt.widgets.Label;
import org.eclipse.ui.menus.WorkbenchWindowControlContribution;

import ch.fusun.baron.basic.client.ui.Activator;
import ch.fusun.baron.core.injection.Inject;
import ch.fusun.baron.core.injection.ReInjector;
import ch.fusun.baron.core.service.UserService;
import ch.fusun.baron.data.DataListener;
import ch.fusun.baron.data.DataUpdate;
import ch.fusun.baron.player.Dynasty;
import ch.fusun.baron.player.api.PlayerService;

/**
 * Shows the coat of arms of the current player
 */
public class CoatOfArmsContributionImage extends
        WorkbenchWindowControlContribution implements DataListener {

    @Inject
    private transient UserService userService;

    @Inject
    private transient PlayerService playerService;

    private transient Label image;

    /**
	 * 
	 */
    public CoatOfArmsContributionImage() {
    }

    /**
     * @param id
     *            The id
     */
    public CoatOfArmsContributionImage(String id) {
        super(id);
    }

    @Override
    protected Control createControl(Composite parent) {
        ReInjector.getInstance().reInject(this);
        image = new Label(parent, SWT.NONE);
        image.setImage(Activator.getImage(Activator.ICONS_EMPTY));
        playerService.addDataListener(this);
        return image;
    }

    @Override
    public void dataChanged(DataUpdate update) {
        if (!image.isDisposed()) {
            image.getDisplay().asyncExec(new Runnable() {

                @Override
                public void run() {
                    Dynasty dynasty = playerService.getDynasty(userService
                            .getUser());
                    image.setImage(Activator.getImageForCOA(dynasty
                            .getCoatOfArms()));
                }
            });
        }
    }
}
